package org.example.ims.Service.Base.Impl;

import org.example.ims.Entity.Base.Category;
import org.example.ims.Entity.Base.Product;
import org.example.ims.Repository.Base.CategoryRepository;
import org.example.ims.Repository.Base.ProductRepository;
import org.example.ims.Service.Base.ProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Service;

import java.util.Optional;

@Service("productService")
public class ProductServiceImpl implements ProductService {

    @Autowired
    private ProductRepository productRepository;

    @Autowired
    private CategoryRepository categoryRepository;

    @Override
    public Product addProduct(Product product) {
        Category category = categoryRepository.findById(product.getCategoryId())
                .orElseThrow(() -> new RuntimeException("Category not found"));
        product.setCategory(category);
        return productRepository.save(product);
    }

    @Override
    public Product addProduct2(Product product) {
        if (!productRepository.existsById(999)) {
            Product temp = new Product();
            temp.setProductId(999); // 设置 ID，但下面会清除
            temp.setProductName("Manual Insert");

            // 强制清空 ID，让 JPA 认为是新实体
            temp.setProductId(null);

            productRepository.save(temp); // 会执行 INSERT
        } else {
            throw new RuntimeException("ID already exists");
        }
        return null;
    }

    @Override
    public Optional<Product> findProductById(Integer productId) {
        return productRepository.findById(productId);
    }

    @Override
    public Page<Product> findAllProducts(Integer page, Integer size) {
        return productRepository.findAll(PageRequest.of(page,size));
    }

    @Override
    public Page<Product> searchProducts(String name, Integer categoryId, String status, Integer page, Integer size) {
        return productRepository.searchProducts(
                name, categoryId, status, PageRequest.of(page, size)
        );
    }

    @Override
    public Product updateProduct(Integer productId, Product productDetails) {
        // Fetch the existing product
        Product product = productRepository.findById(productId)
                .orElseThrow(() -> new RuntimeException("Product not found"));
        //没有使用这里返回的product

        // Fetch the category by categoryId
        Category category = categoryRepository.findById(productDetails.getCategoryId())
                .orElseThrow(() -> new RuntimeException("Category not found"));
        productDetails.setCategory(category);

        return productRepository.save(productDetails);
    }

    @Override
    public void deleteProductById(Integer productId) {
        if (!productRepository.existsById(productId)) {
            throw new RuntimeException("Product does not exist");
        }
        productRepository.deleteById(productId);
    }
}